﻿using LostOrFind.Context;
using LostOrFind.Models;
using LostOrFind.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace LostOrFind.APIs;

[ApiController]
[Route("api/[controller]")]
public class LostOrFindController : Controller
{
    private MyDbContext _dbContext;
    private IOccasionRepository _occasionRepository;
    public LostOrFindController(MyDbContext dbContext, IOccasionRepository occasionRepository)
    {
        _dbContext = dbContext;
        _occasionRepository = occasionRepository;
    }

    [HttpPost]
    [Authorize]
    public async Task<ActionResult<Occasion?>> AddOccasion(Occasion occasion)
    {
        var userName = User.Identity!.Name;
        if (userName!=occasion.User!.Name) return Unauthorized(); // 返回401未授权状态码

        try
        {
            _occasionRepository.AddOccasion(occasion);
            return Ok(occasion); // 返回200 OK状态码和Occasion对象
        }
        catch (Exception e)
        {
            return StatusCode(500, "An error occurred"); // 返回500服务器内部错误状态码
        }
    }

    [HttpPut("{id}")]
    [Authorize]
    public async Task<ActionResult<Occasion>> UpdateOccasion(
        Occasion occasion,int id
    )
    {
        var userName = User.Identity!.Name;
        if (userName!=occasion.User!.Name) return Unauthorized();
        try
        {
            if (id != occasion.Id) return Unauthorized();
            _occasionRepository.UpdateOccasion(occasion);
            return Ok(occasion);
        }
        catch (Exception e)
        {
            return StatusCode(500,$"服务端错误{e}");
        }
    }

    [HttpDelete("{id}")]
    [Authorize]
    public async Task<ActionResult<bool>> DeleteOccasionById(int id)
    {
        var userName = User.Identity!.Name;
        try
        {
            _occasionRepository.DeleteOccasion(id, userName);
            return true;
        }
        catch (UnauthorizedAccessException e1)
        {
            return Unauthorized();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            return StatusCode(500,$"服务端错误 {e}");
        }
    }

    [HttpGet("user/{id}")]
    [Authorize]
    public async Task<IList<Occasion>> GetOccasionsByUserId(int id)
    {
        var occasions = _occasionRepository.GetOccasionByUserId(id);
        foreach (var occasion in occasions)
        {
            occasion.User!.Password = "xxxxxxxx";
        }

        return occasions;
    }

    [HttpGet("{id}")]
    public async Task<Occasion?> GetOccasionById(int id)
    {
        var occasion = _occasionRepository.GetOccasionById(id);
        if (occasion == null) return occasion;
        if (occasion.User != null)
        {
            occasion.User.Password = "xxxxxxxx";
        }

        return occasion;
    }
    
    [HttpGet]
    // [Authorize]
    public async Task<IList<Occasion>> GetRangeOccasion(int from,int to, bool lost)
    {
        return _occasionRepository.GetOccasionByRange(from, to,lost);
    }
}